package com.su.dolphin.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.su.dolphin.DpApp;

/**
 * 网络类型检测
 * 
 * 
 */
public class NetStatusUtil
{
    
    public static boolean isNetAvaliable = false;
    
    public static boolean isConnected() {
        NetworkInfo info = NetStatusUtil.getNetworkInfo(DpApp.getContext());
        isNetAvaliable = (info != null && info.isConnected());
        return isNetAvaliable;
    }
    
    public enum NetWorkType {
        NETWORKTYPE_INVALID, NETWORKTYPE_WIFI, NETWORKTYPE_2G, NETWORKTYPE_3G, NETWORKTYPE_OTHER
    }
    
    /**
     * get network type
     * 
     * @return
     */
    public static NetWorkType getConnectType() {
        if (isConnected()) {
            if (isConnectedWifi()) {
                return NetWorkType.NETWORKTYPE_WIFI;
            }
            else {
                if (isConnectedMobile()) {
                    if (isConnectedFast()) {
                        return NetWorkType.NETWORKTYPE_3G;
                    }
                    else {
                        return NetWorkType.NETWORKTYPE_2G;
                    }
                }
                else {
                    return NetWorkType.NETWORKTYPE_OTHER;
                }
            }
        }
        else {
            return NetWorkType.NETWORKTYPE_INVALID;
        }
    }
    
    public static NetworkInfo getNetworkInfo(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo();
    }
    
    /**
     * Check if there is any connectivity to a Wifi network
     * 
     * @param context
     * @param type
     * @return
     */
    public static boolean isConnectedWifi() {
        NetworkInfo info = NetStatusUtil.getNetworkInfo(DpApp.getContext());
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }
    
    /**
     * Check if there is any connectivity to a mobile network
     * 
     * @param context
     * @param type
     * @return
     */
    public static boolean isConnectedMobile() {
        NetworkInfo info = NetStatusUtil.getNetworkInfo(DpApp.getContext());
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
    }
    
    /**
     * Check if there is fast connectivity
     * 
     * @param context
     * @return
     */
    public static boolean isConnectedFast() {
        NetworkInfo info = NetStatusUtil.getNetworkInfo(DpApp.getContext());
        return (info != null && info.isConnected() && NetStatusUtil.isConnectionFast(info.getType(), info.getSubtype()));
    }
    
    /**
     * Check if the connection is fast
     * 
     * @param type
     * @param subType
     * @return
     */
    public static boolean isConnectionFast(int type, int subType) {
        if (type == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        else if (type == ConnectivityManager.TYPE_MOBILE) {
            switch (subType) {
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                    return false; // ~ 50-100 kbps
                case TelephonyManager.NETWORK_TYPE_CDMA:
                    return false; // ~ 14-64 kbps
                case TelephonyManager.NETWORK_TYPE_EDGE:
                    return false; // ~ 50-100 kbps
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    return true; // ~ 400-1000 kbps
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    return true; // ~ 600-1400 kbps
                case TelephonyManager.NETWORK_TYPE_GPRS:
                    return false; // ~ 100 kbps
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                    return true; // ~ 2-14 Mbps
                case TelephonyManager.NETWORK_TYPE_HSPA:
                    return true; // ~ 700-1700 kbps
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                    return true; // ~ 1-23 Mbps
                case TelephonyManager.NETWORK_TYPE_UMTS:
                    return true; // ~ 400-7000 kbps
                    /*
                     * Above API level 7, make sure to set
                     * android:targetSdkVersion to appropriate level to use
                     * these
                     */
                    // case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11
                    // return true; // ~ 1-2 Mbps
                    // case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9
                    // return true; // ~ 5 Mbps
                    // case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13
                    // return true; // ~ 10-20 Mbps
                case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                    return false; // ~25 kbps
                    // case TelephonyManager.NETWORK_TYPE_LTE: // API level 11
                    // return true; // ~ 10+ Mbps
                    // Unknown
                case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                default:
                    return false;
            }
        }
        else {
            return false;
        }
    }
    
    /**
     * 判断wifi 的设置 是否是一直 链接
     * 
     * @return true一直链接或者当前网络不是wifi，false当前网络是wifi并且设置不是一直链接
     */
    @SuppressWarnings ("deprecation")
    public static boolean isWifiAlwaysConnect(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI) {
            int wifiSleepPolicy = Settings.System.getInt(context.getContentResolver(), "wifi_sleep_policy", Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
            if (wifiSleepPolicy != Settings.System.WIFI_SLEEP_POLICY_NEVER) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 尝试链接移动网络
     * 
     * @param context
     */
    public static void tryToConnectMobile(Context context) {
        boolean checkContiune = true;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        int num = 0;
        while (checkContiune) {
            try {
                Thread.sleep(1000);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
            NetworkInfo[] arrayOfNetworkInfo = connectivityManager.getAllNetworkInfo();
            if (arrayOfNetworkInfo != null && arrayOfNetworkInfo.length > 0) {
                for (int index = 0; index < arrayOfNetworkInfo.length; index++) {
                    if (arrayOfNetworkInfo[index].getState().compareTo(NetworkInfo.State.CONNECTED) == 0) {
                        
                        String type = arrayOfNetworkInfo[index].getTypeName();
                        if (TextUtils.isEmpty(type) && type.contains("wap")) {
                            checkContiune = false;
                            connectivityManager.setNetworkPreference(ConnectivityManager.TYPE_MOBILE);
                            break;
                        }
                    }
                }
            }
            num++;
            if (num > 10) {
                checkContiune = false;
            }
        }
    }
    
    public static String getIp() {
        WifiManager wifimanger = (WifiManager) DpApp.getContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiinfo = wifimanger.getConnectionInfo();
        int i = wifiinfo.getIpAddress();
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "." + (i >> 24 & 0xFF);
    }
}
